package com.example.spirngsecurityverifycode.controller;

import com.google.code.kaptcha.Producer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @Auther: zjh
 * @Date: 2025/9/7 - 09 - 07 - 23:31
 * @Description: com.example.spirngsecurityverifycode.config.controller
 * @version: 1.0
 */
@Controller
@RequestMapping
@RequiredArgsConstructor
public class VerifyController {

    private final Producer producer;
    @RequestMapping("/vc.ing")
    public void verifyCode(HttpServletResponse response, HttpSession session) throws IOException {
        //1.生成验证码
        String verifyCode = producer.createText();
        //2.保存到中 session
        session.setAttribute("kaptcha",verifyCode);
        //3.生成图片
        BufferedImage bi = producer.createImage(verifyCode);
        response.setContentType("image/png");//一定要设置相应格式
        //4.响应图片
        ServletOutputStream os = response.getOutputStream();
        ImageIO.write(bi,"jpg", os);
    }
}
